using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using Boggle.Net.Model;

namespace Boggle.Net.Converters
{
    public class TrayPathToPointCollectionConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(values.Length == 3 && values[1] is double && values[2] is double)) return null;

            var path = values[0] as Tray.Location[];
            if (path == null || path.Length < 2) return null;

            var cellSize = new Size((double)values[1] / Tray.Columns, (double)values[2] / Tray.Rows);
            var offset = new Size(cellSize.Width / 2, cellSize.Height / 2);

            var convertLocationToPoint = new Func<Tray.Location, Point>(location => 
                new Point(
                    location.Column * cellSize.Width + offset.Width,
                    location.Row * cellSize.Height + offset.Height));

            var result = new PointCollection(path.Select(convertLocationToPoint));
            return result;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}